/**
 * Created by RayLew on 2014/11/19.
 */
var doubleLessonMap = new HashMap();

function getDoubleLessonCount() {
    jQuery.ajax({
        type: "GET",
        url: "getClassDoubleLessons?taskId=" + taskId,
        cache: false,
        async: false,
        success: function (msg) {
            var retData = JSON.parse(msg);
            var length = retData.length;
            for (var i = 0; i < length; i++) {
                doubleLessonMap.put(retData[i].classId + "_" + retData[i].courseId, retData[i].doubleLessons);
            }
        }
    });
}

function showDoubleLessonCount() {
    $(".doubleLesson_class td").each(function () {
        var td = $(this);
        if (td.attr("classId") != undefined && td.attr("courseId") != undefined) {
            var value = parseInt(doubleLessonMap.get(td.attr("classId") + "_" + td.attr("courseId")));
            if (value > 0) {
                td.html(value);
            }
        }
    });
}

//function toggleGrade(gradeId) {
//    $("tr[gradeId=" + gradeId + "]").toggle();
//    var $i = $("#i_" + gradeId);
//    if ($i.hasClass('glyphicon-plus-sign')) {
//        $i.removeClass('glyphicon-plus-sign').addClass('glyphicon-minus-sign');
//    } else {
//        $i.removeClass('glyphicon-minus-sign').addClass('glyphicon-plus-sign');
//    }
//}

function toggleGrade(gradeId) {
    $("tr[gradeId=" + gradeId + "]").toggle();
    var $i = $("#i_" + gradeId);
    if ($i.hasClass('glyphicon-chevron-up')) {
        $i.removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
    } else {
        $i.removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
    }
}

/*从此步重新开始*/
function reStartFromHere(where) {
    var ret = reStartFromWhere(where);
    if (ret == true) {
        //to do
        $("#reStartNotice").text('设置成功').fadeOut(5000);
    } else {
        //to do
        $("#reStartNotice").text('设置失败').fadeOut(5000);
    }
    location.reload();
}


function createRule() {
    var amLessonCount = parseInt(timeTable.data.amLessonCount);
    var pmLessonCount = parseInt(timeTable.data.pmLessonCount);
    var eveLessonCount = parseInt(timeTable.data.eveLessonCount);
    var lessonsPerDay = amLessonCount + pmLessonCount + eveLessonCount;
    var html = "";
    //for (i = 1; i < lessonsPerDay; i++) {
    //    var val = (i - 1).toString() + "-" + (i).toString();
    //    var txt = i.toString() + "-" + (i + 1).toString();
    //    html += "<span><input type='checkbox' value='" + val + "' id='chk" + val + "'/><label for='chk" + val + "'>" + txt + "</label></span>"
    //}

    var checkDisabledHtml = "";
    if(!isMyTask){
        checkDisabledHtml = "disabled='disabled'";
    }

    for (i = 1; i < amLessonCount; i++) {
        var val = (i - 1).toString() + "-" + (i).toString();
        var txt = i.toString() + "-" + (i + 1).toString();
        html += "<span><input type='checkbox' value='" + val + "' id='chk" + val + "' "+checkDisabledHtml+"/><label for='chk" + val + "'>" + txt + "</label></span>"
    }

    if(amLessonCount > 0 && pmLessonCount > 0){
        var txt = amLessonCount + "-" + (amLessonCount + 1);
        html += "<span style='color:#ccc' title=\"上午与下午相邻课已自动考虑是不连堂，不需要设置\">" +
            "<input type='checkbox' id=\"am_pm\"/ checked='checked' disabled='disabled'><label for='am_pm'>" + txt + "</label>" +
            "</span>"
    }

    for (i = 1; i < pmLessonCount; i++) {
        var val = (amLessonCount + i - 1).toString() + "-" + (amLessonCount + i ).toString();
        var txt = (amLessonCount + i).toString() + "-" + (amLessonCount + i + 1).toString();
        html += "<span><input type='checkbox' value='" + val + "' id='chk" + val + "' "+checkDisabledHtml+"/><label for='chk" + val + "'>" + txt + "</label></span>"
    }

    if(pmLessonCount > 0 && eveLessonCount > 0){
        var num = amLessonCount + pmLessonCount;
        var txt = num + "-" + (num + 1);
        html += "<span style='color:#ccc' title=\"下午与晚上相邻课已自动考虑是不连堂,不需要设置\">" +
            "<input type='checkbox' id=\"am_pm\"/ checked='checked' disabled='disabled'><label for='am_pm'>" + txt + "</label>" +
            "</span>"
    }

    for (i = 1; i < eveLessonCount; i++) {
        var val = (amLessonCount + pmLessonCount + i - 1).toString() + "-" + (amLessonCount + pmLessonCount + i ).toString();
        var txt = (amLessonCount + pmLessonCount + i).toString() + "-" + (amLessonCount + pmLessonCount + i + 1).toString();
        html += "<span><input type='checkbox' value='" + val + "' id='chk" + val + "' "+checkDisabledHtml+"/><label for='chk" + val + "'>" + txt + "</label></span>"
    }

    $("#doubleLesson").html(html);
    setCheck();
}
//保存未选择的即可连堂课程（与页面逻辑正好相反）
function saveDoubleLesson(func) {
    if(!isMyTask){
        return;
    }

    var val = "";//全部不可连堂。
    $(":checkbox[id^=chk]").not(":checked").each(function () {
        if (val != "") {
            val += "," + $(this).val()
        }
        else {
            val = $(this).val();
        }
    });
    jQuery.ajax({
        type: "POST",
        url: "saveDoubleLessons",
        data: {"taskId": taskId, "doubleLesson": val},
        dataType: "json",
        success: function (msg) {
            if(func){
               func();
            }else {
                //alert("保存成功！");
                $.messager.alert("保存成功！");
            }
        }
    });
}

function setCheck() {
    if (doubleLessons != undefined ) {
        if(doubleLessons != "" && doubleLessons != "0") {
            var val = doubleLessons.split(",");
            $(":checkbox[id^=chk]").each(function () {
                if (!val.contains($(this).val())) {
                    $(this).attr("checked", "true");
                }
            });
        }else if(doubleLessons == ""){
            // 全部不连堂
            $(":checkbox[id^=chk]").each(function () {
                $(this).attr("checked", "true");
            });
        }
    }
}

/*用户关闭页面前自动保存*/
$(window).on('beforeunload', function () {
    saveDoubleLesson(function(){});
    //var val = "0";//0：全部不可连堂。
    //$(":checkbox[id^=chk]").not(":checked").each(function () {
    //    if (val != "0") {
    //        val += "," + $(this).val()
    //    }
    //    else {
    //        val = $(this).val();
    //    }
    //});
    //jQuery.ajax({
    //    type: "POST",
    //    url: "saveDoubleLessons",
    //    data: {"taskId": taskId, "doubleLesson": val},
    //    dataType: "json",
    //    success: function (msg) {
    //        //alert("保存成功！");
    //    }
    //});
});
/*endregion 设置规则生成结果相关*/